<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="css/reset.css">
    <link rel="stylesheet" href="css/iconfont.css">
    <link rel="stylesheet" href="css/main.css">
    <script src="./libs/jquery-1.12.4.min.js"></script>
    <script src="./libs/bootstrap/js/bootstrap.min.js"></script>
    <!-- 引入模板引擎js文件 -->
    <script src="./libs/template-web.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            文章类别管理
        </div>
        <div class="container-fluid common_con">
            <table class="table table-striped table-bordered table-hover mp20 category_table">
                <thead>
                    <tr>
                        <th>名称</th>
                        <th>Slug</th>
                        <th class="text-center" width="100">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>奇趣事</td>
                        <td>funny</td>
                        <td class="text-center">
                            <a href="javascript:void(0)" data-toggle="modal" class=" btn btn-info btn-xs">编辑</a>
                            <a href="javascript:void(0)" class="btn btn-danger btn-xs">删除</a>
                        </td>
                    </tr>
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="3" class="text-center">
                            <a href="javascript:void(0)" class="btn btn-success" id="xinzengfenlei" data-toggle="modal"
                                data-target="#myModal">新增分类</a>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
    <!-- 模态框 -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="exampleModalLabel">New message</h4>
                </div>
                <div class="modal-body">
                    <form>
                        <!-- 隐藏域存放ID -->
                        <input type="hidden" id="hiddenid" name="id" value="">
                        <div class="form-group">
                            <label for="recipient-name" class="control-label">名称:</label>
                            <input type="text" class="form-control" id="recipient-name">
                        </div>
                        <div class="form-group">
                            <label for="message-text" class="control-label">Slug:</label>
                            <input type="text" class="form-control" id="message-text">
                        </div>
                    </form>
                </div>
                <div class="modal-footer">

                    <button type="button" class="btn btn-primary btn-add">Send message</button>
                </div>
            </div>
        </div>
    </div>
    <!-- http://localhost:8080/api/v1 -->
</body>

</html>
<script>
    $(function () {
        //页面加载函数
        function loadData() {
            $.ajax(
                {
                    type: 'get',
                    url: 'http://localhost:8080/api/v1/admin/category/list',
                    success: function (res) {
                        if (res.code == 200) {
                            let htmlStr = ''
                            $.each(res.data, function (index, ele) {
                                htmlStr += `
                            <tr>
                        <td>${ele.name}</td>
                        <td>${ele.slug}</td>
                        <td class="text-center">
                            <a data-id='${ele.id}' href="javascript:void(0)" data-toggle="modal" class=" btn btn-info btn-xs btn-edit" data-toggle="modal" data-target="#myModal">编辑</a>
                            <a data-id='${ele.id}' href="javascript:void(0)" class="btn btn-danger btn-xs btn-del">删除</a>
                        </td>
                    </tr>
                            `
                            })
                            $('tbody').html(htmlStr)
                        }
                    }
                }
            )
        }

        loadData()
        //模态框事件
        $('#myModal').on('show.bs.modal', function (e) {
            $('#recipient-name').val('')
            $('#message-text').val('')
            if ($(e.relatedTarget).text() == '新增分类') {
                $('#exampleModalLabel').text('新增分类')
                $('.btn-primary').text('新增').addClass('btn-success').removeClass('btn-info')
            } else {
                $('#exampleModalLabel').text('编辑分类')
                $('.btn-primary').text('编辑').addClass('btn-info').removeClass('btn-success')
                $('#hiddenid').val($(e.relatedTarget).attr('data-id'))
            }
        })
        //给模态框按钮注册点击事件
        $('.btn-add').on('click', function () {
            let name = $('#recipient-name').val()
            let slug = $('#message-text').val()
            if (name == '' || slug == '') {
                alert('请输入内容');
                return $('#myModal').modal('hide')
            }
            if ($(this).text() == '新增') {

                $.ajax(
                    {
                        type: 'post',
                        url: 'http://localhost:8080/api/v1/admin/category/add',
                        data: {
                            name: name,
                            slug: slug
                        },
                        success: function (res) {
                            console.log(res);
                            $('#myModal').modal('hide')
                            loadData()
                        }
                    }
                )
            } else {
                let id = $('#hiddenid').val()
                let name = $('#recipient-name').val()
                let slug = $('#message-text').val()
                $.ajax(
                    {
                        type: 'post',
                        url: 'http://localhost:8080/api/v1/admin/category/edit',
                        data: {
                            id: id,
                            name: name,
                            slug: slug
                        },
                        success: function (res) {
                            console.log(res);
                            $('#myModal').modal('hide')
                            loadData()
                        }
                    }
                )
            }
            // $('#myModal').modal('hide')
        })
        //给编辑按钮注册点击事件
        $('tbody').on('click', '.btn-edit', function () {
            let id = $(this).attr('data-id')
            $.ajax(
                {
                    type: 'get',
                    url: 'http://localhost:8080/api/v1/admin/category/search',
                    data: {
                        id: id
                    },
                    success: function (res) {
                        $('#recipient-name').val(res.data[0].name)
                        $('#message-text').val(res.data[0].slug)
                    }
                }
            )
        })
        //给删除按钮注册点击事件
        $('tbody').on('click','.btn-del',function(){
            let id = $(this).attr('data-id')
            let option = confirm('是否确认删除')
            if(option){
                $.ajax(
                    {
                        type:'post',
                        url:'http://localhost:8080/api/v1/admin/category/delete',
                        data: {
                            id:id
                        },
                        success: function (res) {
                            loadData()
                        }
                    }
                )
            }else{

            }
        })
    })
</script>